import pymysql
from pymysql import cursors
from Config.db_config import ticketDB


class DBUtil:
    @staticmethod
    def openDB():
        connObj = pymysql.connect(host=ticketDB['host'],port=ticketDB['port'],user=ticketDB['user'],password=ticketDB['passwd'],db=ticketDB['db'],charset='utf8')
        cursor = connObj.cursor(cursors.DictCursor)
        return connObj,cursor
    @staticmethod
    def readOne(sql,cursor):
        cursor.execute(sql)
        record = cursor.fetchone()
        return record
    @staticmethod
    def readAll(sql,cursor):
        cursor.execute(sql)
        records = cursor.fetchall()
        return records
    @staticmethod
    def writeDB(sql,cursor):
        return cursor.execute(sql)

    @staticmethod
    def release(cursor,connObj):
        if cursor is not None:
            cursor.close()
        if connObj is not None:
            connObj.close()
if __name__ == '__main__':
    connObj,cursor = DBUtil.openDB()
    sql = 'SELECT * FROM tb_actor'
    print(connObj)
    print(cursor)
    print(DBUtil.readAll(sql,cursor))

    sql = "UPDATE tb_actor SET actor_name = 'worldwind_actor1' WHERE actor_id = 262"
    print(DBUtil.writeDB(sql,cursor))
    connObj.commit()
    print(DBUtil.release(cursor, connObj))